package com.assignwork.modules.pc.service.pc.impl;
import com.assignwork.dto.pcplan.PcShipPlanSearchDto;
import com.assignwork.dto.pcplan.PcShipPlanSubmitDto;
import com.assignwork.entity.VoyageInfo;
import com.assignwork.entity.pc.PcShipEfficiency;
import com.assignwork.entity.pc.PcShipInfoEntering;
import com.assignwork.entity.pc.PcShipPlan;
import com.assignwork.mapper.PcShipInfoEnteringMapper;
import com.assignwork.mapper.PcShipPlanMapper;
import com.assignwork.modules.pc.service.pc.IPcShipPlanService;
import com.assignwork.vo.pc.PcShipPlanVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@AllArgsConstructor
public class PcShipPlanServiceImpl extends BaseServiceImpl<PcShipPlanMapper, PcShipPlan> implements IPcShipPlanService {
    private PcShipInfoEnteringMapper pcShipInfoEnteringMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean submitPcShipPlan(PcShipPlanSubmitDto submitDTO) {

        PcShipInfoEntering pcShipInfoEntering = pcShipInfoEnteringMapper.selectById(submitDTO.getShipId());
        pcShipInfoEntering.setIsPcPlan(1);
        pcShipInfoEntering.setBerthId(submitDTO.getBerthId());
        PcShipInfoEntering copyPcShipInfoEntering = BeanUtil.copy(pcShipInfoEntering, PcShipInfoEntering.class);
        pcShipInfoEnteringMapper.updateById(copyPcShipInfoEntering);


        PcShipPlan copy = BeanUtil.copy(submitDTO, PcShipPlan.class);
        if (Func.notNull(copy.getIsMoveBerth()) && copy.getIsMoveBerth() == 1) {
            PcShipPlan pcShipPlan = lambdaQuery().eq(PcShipPlan::getShipId, copy.getShipId()).eq(PcShipPlan::getIsMoveBerth, 0).eq(PcShipPlan::getIsDeleted, 0).one();
            pcShipPlan.setOutBerthTime(copy.getInBerthTime());
            super.saveOrUpdate(pcShipPlan);
        }
        super.saveOrUpdate(copy);
        return true;
    }

    @Override
    public List<PcShipPlanVo> selectPcShipEfficiencyList(PcShipPlanSearchDto dto) {
        List<PcShipPlanVo> pcShipPlanVos = baseMapper.selectPcShipEfficiencyList(dto);
        return pcShipPlanVos;
    }
}
